﻿Public Class 商品入库查询

    Private database As DataAccess
    Private data_In As DataTable

    Private Sub 商品入库查询_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        database = New DataAccess
        database.OpenConnection()

        Dim sqlString As String
        sqlString = "select * from tf_in_goods"
        data_In = database.QueryDatabase(sqlString)

        '初始化商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

    End Sub

    Private Sub GoodTypeList_Init()

        ComboBox_Good_Type.Items.Clear()

        '将商品类型数据列在下拉菜单中
        Dim row As DataRow
        For Each row In data_In.Rows
            'MessageBox.Show("data_Good.Rows1")
            Dim good_type As String
            good_type = row("goods_type")

            ' 去除重复项
            If ComboBox_Good_Type.FindString(good_type) = -1 Then
                ComboBox_Good_Type.Items.Add(good_type)
            End If

        Next row

        'If ComboBox_Good_Type.Items.Count > 0 Then
        'ComboBox_Good_Type.SelectedIndex = 0
        'End If

    End Sub

    Private Sub GoodNameList_Init()

        ComboBox_Good_Name.Items.Clear()

        '从数据库中获取商品名称信息
        Dim good_type As String = ComboBox_Good_Type.Text
        'Dim sqlString As String
        'sqlString = "select goods_name from tf_f_goods where goods_type ='" & good_type & "'"
        'data_Good = database.QueryDatabase(sqlString)

        Dim rows() As DataRow
        rows = data_In.Select("goods_type ='" & good_type & "'")

        '将商品名称数据列在下拉菜单中
        Dim row As DataRow
        For Each row In rows
            ComboBox_Good_Name.Items.Add(row("goods_name"))
        Next row

        'If ComboBox_Good_Name.Items.Count > 0 Then
        'ComboBox_Good_Name.SelectedIndex = 0
        'End If

    End Sub

    Private Sub ComboBox_Good_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Type.SelectedIndexChanged

        ComboBox_Good_Name.Text = ""

        '更新商品名称列表
        GoodNameList_Init()

    End Sub

    Private Sub Button_Query_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Query.Click

        Dim good_type As String
        Dim good_name As String

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text

        query_in_good_record(good_type, good_name)

    End Sub

    Private Function query_in_good_record(ByVal good_type As String, ByVal good_name As String) As Boolean

        '生成数据过滤条件
        Dim filter As String = ""
        If good_type <> "" Then
            filter = filter & " goods_type='" & good_type & "'"
        End If
        If good_name <> "" Then
            filter = filter & " and goods_name='" & good_name & "'"
        End If

        If CheckBox_Date_Time.Checked = True Then

            If filter <> "" Then
                filter = filter & " and "
            End If

            filter = filter & "in_date>'" & DateTimePicker_Start.Value & "' and in_date<'" _
                            & DateTimePicker_End.Value & "'"

        End If

        '在dataview中显示过滤数据
        Dim dv As DataView
        dv = data_In.DefaultView
        DataGridView_Query.DataSource = dv
        dv.RowFilter = filter

        '修改列标题
        DataGridView_Query.Columns(0).HeaderText = "入库批次"
        DataGridView_Query.Columns(1).HeaderText = "商品编号"
        DataGridView_Query.Columns(2).HeaderText = "商品种类"
        DataGridView_Query.Columns(3).HeaderText = "商品名称"
        DataGridView_Query.Columns(4).HeaderText = "进货价"
        DataGridView_Query.Columns(5).HeaderText = "批发价"
        DataGridView_Query.Columns(6).HeaderText = "零售价"
        DataGridView_Query.Columns(7).HeaderText = "入库数量"
        DataGridView_Query.Columns(8).HeaderText = "计量单位"
        DataGridView_Query.Columns(9).HeaderText = "上家联系电话"
        DataGridView_Query.Columns(10).HeaderText = "货源地"
        DataGridView_Query.Columns(11).HeaderText = "进货日期"
        DataGridView_Query.Columns(12).HeaderText = "修改时间"
        DataGridView_Query.Columns(13).HeaderText = "备注"

        '隐藏批次ID列表
        DataGridView_Query.Columns(1).Visible = False

        Return True
    End Function

End Class